﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 文件传输客户端基类，提供上传，下载，删除的基础服务<br />
	/// File transfer client base class, providing basic services for uploading, downloading, and deleting
	/// </summary>
	// Token: 0x020000F5 RID: 245
	public abstract class FileClientBase : NetworkXBase
	{
		/// <summary>
		/// 文件管理服务器的ip地址及端口<br />
		/// IP address and port of the file management server
		/// </summary>
		// Token: 0x170004F0 RID: 1264
		// (get) Token: 0x060014AC RID: 5292 RVA: 0x0006AEB0 File Offset: 0x000690B0
		// (set) Token: 0x060014AD RID: 5293 RVA: 0x0006AEC8 File Offset: 0x000690C8
		public IPEndPoint ServerIpEndPoint
		{
			get
			{
				return this.m_ipEndPoint;
			}
			set
			{
				this.m_ipEndPoint = value;
			}
		}

		/// <summary>
		/// 获取或设置连接的超时时间，默认10秒<br />
		/// Gets or sets the connection timeout time. The default is 10 seconds.
		/// </summary>
		// Token: 0x170004F1 RID: 1265
		// (get) Token: 0x060014AE RID: 5294 RVA: 0x0006AED2 File Offset: 0x000690D2
		// (set) Token: 0x060014AF RID: 5295 RVA: 0x0006AEDA File Offset: 0x000690DA
		public int ConnectTimeOut { get; set; } = 10000;

		/// <summary>
		/// 发送三个文件分类信息到服务器端，方便后续开展其他的操作。<br />
		/// Send the three file classification information to the server to facilitate subsequent operations.
		/// </summary>
		/// <param name="socket">套接字对象</param>
		/// <param name="factory">一级分类</param>
		/// <param name="group">二级分类</param>
		/// <param name="id">三级分类</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x060014B0 RID: 5296 RVA: 0x0006AEE4 File Offset: 0x000690E4
		protected OperateResult SendFactoryGroupId(Socket socket, string factory, string group, string id)
		{
			OperateResult operateResult = base.SendStringAndCheckReceive(socket, 1, factory);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.SendStringAndCheckReceive(socket, 2, group);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = base.SendStringAndCheckReceive(socket, 3, id);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.FileClientBase.SendFactoryGroupId(System.Net.Sockets.Socket,System.String,System.String,System.String)" />
		// Token: 0x060014B1 RID: 5297 RVA: 0x0006AF58 File Offset: 0x00069158
		[DebuggerStepThrough]
		protected Task<OperateResult> SendFactoryGroupIdAsync(Socket socket, string factory, string group, string id)
		{
			FileClientBase.<SendFactoryGroupIdAsync>d__9 <SendFactoryGroupIdAsync>d__ = new FileClientBase.<SendFactoryGroupIdAsync>d__9();
			<SendFactoryGroupIdAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<SendFactoryGroupIdAsync>d__.<>4__this = this;
			<SendFactoryGroupIdAsync>d__.socket = socket;
			<SendFactoryGroupIdAsync>d__.factory = factory;
			<SendFactoryGroupIdAsync>d__.group = group;
			<SendFactoryGroupIdAsync>d__.id = id;
			<SendFactoryGroupIdAsync>d__.<>1__state = -1;
			<SendFactoryGroupIdAsync>d__.<>t__builder.Start<FileClientBase.<SendFactoryGroupIdAsync>d__9>(ref <SendFactoryGroupIdAsync>d__);
			return <SendFactoryGroupIdAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 删除服务器上的文件，需要传入文件信息，以及文件绑定的分类信息。<br />
		/// To delete a file on the server, you need to pass in the file information and the classification information of the file binding.
		/// </summary>
		/// <param name="fileName">文件的名称</param>
		/// <param name="factory">一级分类</param>
		/// <param name="group">二级分类</param>
		/// <param name="id">三级分类</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x060014B2 RID: 5298 RVA: 0x0006AFBC File Offset: 0x000691BC
		protected OperateResult DeleteFileBase(string fileName, string factory, string group, string id)
		{
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ServerIpEndPoint, this.ConnectTimeOut);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.SendStringAndCheckReceive(operateResult.Content, 2003, fileName);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = this.SendFactoryGroupId(operateResult.Content, factory, group, id);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						OperateResult<int, string> operateResult4 = base.ReceiveStringContentFromSocket(operateResult.Content, 30000);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = operateResult4;
						}
						else
						{
							OperateResult operateResult5 = new OperateResult();
							bool flag5 = operateResult4.Content1 == 1;
							if (flag5)
							{
								operateResult5.IsSuccess = true;
							}
							operateResult5.Message = operateResult4.Message;
							Socket content = operateResult.Content;
							if (content != null)
							{
								content.Close();
							}
							result = operateResult5;
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 删除服务器上的文件列表，需要传入文件信息，以及文件绑定的分类信息。<br />
		/// To delete a file on the server, you need to pass in the file information and the classification information of the file binding.
		/// </summary>
		/// <param name="fileNames">所有等待删除的文件的名称</param>
		/// <param name="factory">一级分类</param>
		/// <param name="group">二级分类</param>
		/// <param name="id">三级分类</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x060014B3 RID: 5299 RVA: 0x0006B0B0 File Offset: 0x000692B0
		protected OperateResult DeleteFileBase(string[] fileNames, string factory, string group, string id)
		{
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ServerIpEndPoint, this.ConnectTimeOut);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.SendStringAndCheckReceive(operateResult.Content, 2011, fileNames);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = this.SendFactoryGroupId(operateResult.Content, factory, group, id);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						OperateResult<int, string> operateResult4 = base.ReceiveStringContentFromSocket(operateResult.Content, 30000);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = operateResult4;
						}
						else
						{
							OperateResult operateResult5 = new OperateResult();
							bool flag5 = operateResult4.Content1 == 1;
							if (flag5)
							{
								operateResult5.IsSuccess = true;
							}
							operateResult5.Message = operateResult4.Message;
							Socket content = operateResult.Content;
							if (content != null)
							{
								content.Close();
							}
							result = operateResult5;
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 删除服务器上的指定目录的所有文件，需要传入分类信息。<br />
		/// To delete all files in the specified directory on the server, you need to input classification information
		/// </summary>
		/// <param name="factory">一级分类</param>
		/// <param name="group">二级分类</param>
		/// <param name="id">三级分类</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x060014B4 RID: 5300 RVA: 0x0006B1A4 File Offset: 0x000693A4
		protected OperateResult DeleteFolder(string factory, string group, string id)
		{
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ServerIpEndPoint, this.ConnectTimeOut);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.SendStringAndCheckReceive(operateResult.Content, 2012, "");
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = this.SendFactoryGroupId(operateResult.Content, factory, group, id);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						OperateResult<int, string> operateResult4 = base.ReceiveStringContentFromSocket(operateResult.Content, 30000);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = operateResult4;
						}
						else
						{
							OperateResult operateResult5 = new OperateResult();
							bool flag5 = operateResult4.Content1 == 1;
							if (flag5)
							{
								operateResult5.IsSuccess = true;
							}
							operateResult5.Message = operateResult4.Message;
							Socket content = operateResult.Content;
							if (content != null)
							{
								content.Close();
							}
							result = operateResult5;
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.FileClientBase.DeleteFileBase(System.String,System.String,System.String,System.String)" />
		// Token: 0x060014B5 RID: 5301 RVA: 0x0006B29C File Offset: 0x0006949C
		[DebuggerStepThrough]
		protected Task<OperateResult> DeleteFileBaseAsync(string fileName, string factory, string group, string id)
		{
			FileClientBase.<DeleteFileBaseAsync>d__13 <DeleteFileBaseAsync>d__ = new FileClientBase.<DeleteFileBaseAsync>d__13();
			<DeleteFileBaseAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<DeleteFileBaseAsync>d__.<>4__this = this;
			<DeleteFileBaseAsync>d__.fileName = fileName;
			<DeleteFileBaseAsync>d__.factory = factory;
			<DeleteFileBaseAsync>d__.group = group;
			<DeleteFileBaseAsync>d__.id = id;
			<DeleteFileBaseAsync>d__.<>1__state = -1;
			<DeleteFileBaseAsync>d__.<>t__builder.Start<FileClientBase.<DeleteFileBaseAsync>d__13>(ref <DeleteFileBaseAsync>d__);
			return <DeleteFileBaseAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.FileClientBase.DeleteFileBase(System.String[],System.String,System.String,System.String)" />
		// Token: 0x060014B6 RID: 5302 RVA: 0x0006B300 File Offset: 0x00069500
		[DebuggerStepThrough]
		protected Task<OperateResult> DeleteFileBaseAsync(string[] fileNames, string factory, string group, string id)
		{
			FileClientBase.<DeleteFileBaseAsync>d__14 <DeleteFileBaseAsync>d__ = new FileClientBase.<DeleteFileBaseAsync>d__14();
			<DeleteFileBaseAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<DeleteFileBaseAsync>d__.<>4__this = this;
			<DeleteFileBaseAsync>d__.fileNames = fileNames;
			<DeleteFileBaseAsync>d__.factory = factory;
			<DeleteFileBaseAsync>d__.group = group;
			<DeleteFileBaseAsync>d__.id = id;
			<DeleteFileBaseAsync>d__.<>1__state = -1;
			<DeleteFileBaseAsync>d__.<>t__builder.Start<FileClientBase.<DeleteFileBaseAsync>d__14>(ref <DeleteFileBaseAsync>d__);
			return <DeleteFileBaseAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.FileClientBase.DeleteFolder(System.String,System.String,System.String)" />
		// Token: 0x060014B7 RID: 5303 RVA: 0x0006B364 File Offset: 0x00069564
		[DebuggerStepThrough]
		protected Task<OperateResult> DeleteFolderAsync(string factory, string group, string id)
		{
			FileClientBase.<DeleteFolderAsync>d__15 <DeleteFolderAsync>d__ = new FileClientBase.<DeleteFolderAsync>d__15();
			<DeleteFolderAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<DeleteFolderAsync>d__.<>4__this = this;
			<DeleteFolderAsync>d__.factory = factory;
			<DeleteFolderAsync>d__.group = group;
			<DeleteFolderAsync>d__.id = id;
			<DeleteFolderAsync>d__.<>1__state = -1;
			<DeleteFolderAsync>d__.<>t__builder.Start<FileClientBase.<DeleteFolderAsync>d__15>(ref <DeleteFolderAsync>d__);
			return <DeleteFolderAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 下载服务器的文件数据，并且存储到对应的内容里去。<br />
		/// Download the file data of the server and store it in the corresponding content.
		/// </summary>
		/// <param name="factory">一级分类</param>
		/// <param name="group">二级分类</param>
		/// <param name="id">三级分类</param>
		/// <param name="fileName">服务器的文件名称</param>
		/// <param name="processReport">下载的进度报告，第一个数据是已完成总接字节数，第二个数据是总字节数。</param>
		/// <param name="source">数据源信息，决定最终存储到哪里去</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x060014B8 RID: 5304 RVA: 0x0006B3C0 File Offset: 0x000695C0
		protected OperateResult DownloadFileBase(string factory, string group, string id, string fileName, Action<long, long> processReport, object source)
		{
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ServerIpEndPoint, this.ConnectTimeOut);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.SendStringAndCheckReceive(operateResult.Content, 2001, fileName);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = this.SendFactoryGroupId(operateResult.Content, factory, group, id);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						string text = source as string;
						bool flag4 = text != null;
						if (flag4)
						{
							OperateResult operateResult4 = base.ReceiveFileFromSocket(operateResult.Content, text, processReport);
							bool flag5 = !operateResult4.IsSuccess;
							if (flag5)
							{
								return operateResult4;
							}
						}
						else
						{
							Stream stream = source as Stream;
							bool flag6 = stream != null;
							if (!flag6)
							{
								Socket content = operateResult.Content;
								if (content != null)
								{
									content.Close();
								}
								ILogNet logNet = base.LogNet;
								if (logNet != null)
								{
									logNet.WriteError(this.ToString(), StringResources.Language.NotSupportedDataType);
								}
								return new OperateResult(StringResources.Language.NotSupportedDataType);
							}
							OperateResult operateResult5 = base.ReceiveFileFromSocket(operateResult.Content, stream, processReport);
							bool flag7 = !operateResult5.IsSuccess;
							if (flag7)
							{
								return operateResult5;
							}
						}
						Socket content2 = operateResult.Content;
						if (content2 != null)
						{
							content2.Close();
						}
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.FileClientBase.DownloadFileBase(System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64},System.Object)" />
		// Token: 0x060014B9 RID: 5305 RVA: 0x0006B534 File Offset: 0x00069734
		[DebuggerStepThrough]
		protected Task<OperateResult> DownloadFileBaseAsync(string factory, string group, string id, string fileName, Action<long, long> processReport, object source)
		{
			FileClientBase.<DownloadFileBaseAsync>d__17 <DownloadFileBaseAsync>d__ = new FileClientBase.<DownloadFileBaseAsync>d__17();
			<DownloadFileBaseAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<DownloadFileBaseAsync>d__.<>4__this = this;
			<DownloadFileBaseAsync>d__.factory = factory;
			<DownloadFileBaseAsync>d__.group = group;
			<DownloadFileBaseAsync>d__.id = id;
			<DownloadFileBaseAsync>d__.fileName = fileName;
			<DownloadFileBaseAsync>d__.processReport = processReport;
			<DownloadFileBaseAsync>d__.source = source;
			<DownloadFileBaseAsync>d__.<>1__state = -1;
			<DownloadFileBaseAsync>d__.<>t__builder.Start<FileClientBase.<DownloadFileBaseAsync>d__17>(ref <DownloadFileBaseAsync>d__);
			return <DownloadFileBaseAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 上传文件给服务器，需要指定上传的数据内容，上传到服务器的分类信息，支持进度汇报功能。<br />
		/// To upload files to the server, you need to specify the content of the uploaded data, 
		/// the classification information uploaded to the server, and support the progress report function.
		/// </summary>
		/// <param name="source">数据源，可以是文件名，也可以是数据流</param>
		/// <param name="serverName">在服务器保存的文件名，不包含驱动器路径</param>
		/// <param name="factory">一级分类</param>
		/// <param name="group">二级分类</param>
		/// <param name="id">三级分类</param>
		/// <param name="fileTag">文件的描述</param>
		/// <param name="fileUpload">文件的上传人</param>
		/// <param name="processReport">汇报进度，第一个数据是已完成总接字节数，第二个数据是总字节数。</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x060014BA RID: 5306 RVA: 0x0006B5A8 File Offset: 0x000697A8
		protected OperateResult UploadFileBase(object source, string serverName, string factory, string group, string id, string fileTag, string fileUpload, Action<long, long> processReport)
		{
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ServerIpEndPoint, this.ConnectTimeOut);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = base.SendStringAndCheckReceive(operateResult.Content, 2002, serverName);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = this.SendFactoryGroupId(operateResult.Content, factory, group, id);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						string text = source as string;
						bool flag4 = text != null;
						if (flag4)
						{
							OperateResult operateResult4 = base.SendFileAndCheckReceive(operateResult.Content, text, serverName, fileTag, fileUpload, processReport);
							bool flag5 = !operateResult4.IsSuccess;
							if (flag5)
							{
								return operateResult4;
							}
						}
						else
						{
							Stream stream = source as Stream;
							bool flag6 = stream != null;
							if (!flag6)
							{
								Socket content = operateResult.Content;
								if (content != null)
								{
									content.Close();
								}
								ILogNet logNet = base.LogNet;
								if (logNet != null)
								{
									logNet.WriteError(this.ToString(), StringResources.Language.DataSourceFormatError);
								}
								return new OperateResult(StringResources.Language.DataSourceFormatError);
							}
							OperateResult operateResult5 = base.SendFileAndCheckReceive(operateResult.Content, stream, serverName, fileTag, fileUpload, processReport);
							bool flag7 = !operateResult5.IsSuccess;
							if (flag7)
							{
								return operateResult5;
							}
						}
						OperateResult<int, string> operateResult6 = base.ReceiveStringContentFromSocket(operateResult.Content, 30000);
						bool flag8 = !operateResult6.IsSuccess;
						if (flag8)
						{
							result = operateResult6;
						}
						else
						{
							result = ((operateResult6.Content1 == 1) ? OperateResult.CreateSuccessResult() : new OperateResult(StringResources.Language.ServerFileCheckFailed));
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.FileClientBase.UploadFileBase(System.Object,System.String,System.String,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})" />
		// Token: 0x060014BB RID: 5307 RVA: 0x0006B758 File Offset: 0x00069958
		[DebuggerStepThrough]
		protected Task<OperateResult> UploadFileBaseAsync(object source, string serverName, string factory, string group, string id, string fileTag, string fileUpload, Action<long, long> processReport)
		{
			FileClientBase.<UploadFileBaseAsync>d__19 <UploadFileBaseAsync>d__ = new FileClientBase.<UploadFileBaseAsync>d__19();
			<UploadFileBaseAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<UploadFileBaseAsync>d__.<>4__this = this;
			<UploadFileBaseAsync>d__.source = source;
			<UploadFileBaseAsync>d__.serverName = serverName;
			<UploadFileBaseAsync>d__.factory = factory;
			<UploadFileBaseAsync>d__.group = group;
			<UploadFileBaseAsync>d__.id = id;
			<UploadFileBaseAsync>d__.fileTag = fileTag;
			<UploadFileBaseAsync>d__.fileUpload = fileUpload;
			<UploadFileBaseAsync>d__.processReport = processReport;
			<UploadFileBaseAsync>d__.<>1__state = -1;
			<UploadFileBaseAsync>d__.<>t__builder.Start<FileClientBase.<UploadFileBaseAsync>d__19>(ref <UploadFileBaseAsync>d__);
			return <UploadFileBaseAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x060014BC RID: 5308 RVA: 0x0006B7D9 File Offset: 0x000699D9
		public override string ToString()
		{
			return string.Format("FileClientBase[{0}]", this.m_ipEndPoint);
		}

		// Token: 0x040004F0 RID: 1264
		private IPEndPoint m_ipEndPoint = null;
	}
}
